package com.eveinfo.display.asset;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.eveinfo.EveInfo;
import com.eveinfo.R;
import com.eveinfo.sql.AllPicturesCache;
import com.eveinfo.sql.data.Item;
import com.eveinfo.tools.ActivityLauncher;
import com.eveinfo.tools.Tools;

public class AssetsByItemsAdapter extends BaseAdapter {

	private Context context;

	private final ArrayList<Item> assets;
	private Item[] contents;

	public AssetsByItemsAdapter(Context context, ListView listView, ArrayList<Item> items) {
		this.context = context;
		this.assets = items;
		setSort(new GroupComparator<Item>());
		this.contents = new Item[assets.size()];
		this.contents = assets.toArray(this.contents);
		listView.setAdapter(this);
		AllPicturesCache allPicturesCache = new AllPicturesCache(items, EveInfo.getPreferences()
				.getAssetsImageSize());
		allPicturesCache.start();
	}
	
	@Override
	public int getCount() {
		return contents.length;
	}

	@Override
	public Object getItem(int position) {
		return contents[position];
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View view = convertView;
		if (view == null) {
			LayoutInflater vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			view = vi.inflate(R.layout.itemr2, null);
		}

		final Item type = contents[position];

		ImageView imageIV = (ImageView) view.findViewById(R.id.IVItemR2Picture);
		imageIV.setImageDrawable(Tools.getItemPicture(type, EveInfo.getPreferences().getAssetsImageSize()));

		TextView nameTV = (TextView) view.findViewById(R.id.TVItemR2Name);
		nameTV.setText(type.getTypeName());
		nameTV.setTextColor(Color.WHITE);
		
		TextView numberTV = (TextView) view.findViewById(R.id.TVItemR2Number);
		numberTV.setText("" + type.getQuantity());
		numberTV.setTextColor(Color.WHITE);

		view.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				ActivityLauncher.displayItem(context, type.getTypeID());
			}
		});
		view.setOnLongClickListener(new OnLongClickListener() {
			@Override
			public boolean onLongClick(View v) {
				ActivityLauncher.displayItemInfo(context, type.getTypeID());
				return true;
			}
		});
		return view;
	}

	public void setSeach(String text) {
		ArrayList<Item> list = new ArrayList<Item>();
		ArrayList<Item> assetsLocal = assets;
		int size = assetsLocal.size();
		for (int i = 0; i < size; i++) {
			Item type = assetsLocal.get(i);
			if (text.equals("")) {
				list.add(type);
			} else {
				String typeName = type.getTypeName();
				int length = text.length();
				if (typeName.regionMatches(true, 0, text, 0, length)) {
					list.add(type);
					continue;
				}
				String[] split = typeName.split(" ");
				for (String s : split) {
					if (s.regionMatches(true, 0, text, 0, length)) {
						list.add(type);
						break;
					}
				}
			}
		}
		this.contents = null;
		this.contents = new Item[list.size()];
		this.contents = list.toArray(this.contents);
		notifyDataSetChanged();
	}

	public void setSort(Comparator<Item> sorter) {
		Collections.sort(assets, sorter);
	}

//	public void setTypes(ArrayList<Item> items) {
//		this.assets.addAll(items);
//		setSort(new GroupComparator<Item>());
//		this.contents = new Item[assets.size()];
//		this.contents = assets.toArray(this.contents);
//		AllPicturesCache allPicturesCache = new AllPicturesCache(items, EveInfo.getPreferences()
//				.getAssetsImageSize());
//		allPicturesCache.start();
//		notifyDataSetChanged();
//	}
}
